package com.tjetc.impl;

import com.tjetc.dao.UserMapper;
import com.tjetc.domian.User;
import com.tjetc.service.UserService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

public class UserServiceImpl implements UserService {
    private UserMapper userMapper= Db.getMapper(UserMapper.class);
    private Result result=new Result();
    @Override
    public Result login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        User user1 = userMapper.findByUsername(username);
        if (user1 == null){
            return result.setCode(500).setMsg("用户名不存在!");
        }else if (user1.getPassword().equals(password)){
            return result.setCode(200).setMsg("登录成功!").setData(user1);
        }else {
            return result.setCode(500).setMsg("密码错误!");
        }
    }

    @Override
    public Result add(User user) {
        try {
            //判重
            User user1 = userMapper.findByUsername(user.getUsername());
            if (user1 != null){
                return result.setCode(500).setMsg("用户名已存在!");
            }
            int rows = userMapper.add(user);
            return rows == 1 ? result.setCode(200).setMsg("用户注册成功!") : result.setCode(500).setMsg("用户注册失败!");

        } catch (Exception e){
            e.printStackTrace();
        }
        return result.setCode(500).setMsg("用户注册失败!");
    }

}
